﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;

public class SystemManager : MonoBehaviour
{
    public enum NetworkMode
    {
        MODE_COMMON,
        MODE_STEAM,
    }
     
    public OnlineManager OnlineManagerCommonPerfab;
    public OnlineManager OnlineManagerSteamPerfab;

    public NetworkMode m_NetworkMode = NetworkMode.MODE_COMMON;
    private void Awake()
    {
        Time.timeScale = 1;
        BulletPool.get().Init();
        SoundsPool.get().Init();
        InfluenceMG.get().Init();
        ItemFactory.Instance.Init();
        ShipManager.Instance.Init();

        Commander.Instance.Miao();  // 初始化一下
        CharacterFactory.Instance.Miao();
        StockFactory.Instance.Miao();
        SceneManager.sceneLoaded += Player.Instance.OnSceneLoaded;
        SceneManager.sceneUnloaded += Player.Instance.OnUnSceneLoaded;

        if (m_NetworkMode == NetworkMode.MODE_COMMON && OnlineManager.singleton == null)
            Instantiate(OnlineManagerCommonPerfab);
        if (m_NetworkMode == NetworkMode.MODE_STEAM && OnlineManager.singleton == null)
            Instantiate(OnlineManagerSteamPerfab);
    }

    // Start is called before the first frame update
    void Start()
    {
    }

    // Update is called once per frame
    void Update()
    {
        MessageShow.Instance.Update();
    }

    private void FixedUpdate()
    {
        if(Player.Instance.m_UIScale >0)
        {
            var canvas = UtilsUnity.GetCanvas();
            if (canvas && Mathf.Abs(canvas.scaleFactor - Player.Instance.m_UIScale) > 0.01f)
                canvas.scaleFactor = Player.Instance.m_UIScale;
        }
        if (Player.Instance.m_UIScale <= 0)
        {
            var canvas = UtilsUnity.GetCanvas();
            if (canvas)
            {
                if(Screen.height < 1080)
                    canvas.scaleFactor = 1080 / Screen.height;
                else
                    canvas.scaleFactor = 2f;
            }
        }
    }

    private void OnDestroy()
    {
        ItemFactory.Instance.Clear();
        InfluenceMG.get().Clear();
        SceneManager.sceneLoaded -= Player.Instance.OnSceneLoaded;

        BulletPool.get().OnSceneUnloaded();
        SoundsPool.get().OnSceneUnloaded();
    }
}
